<?php


namespace app\model;


use think\Model;

class StoreBail extends Model
{
    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'store_bail';


    public static function addBail($data){
        if($data['type'] == 1) {//充值
            Store::where(['id'=>$data['sid']])->inc('bail',$data['amount'])->update();
        }else if ($data['type'] == 2) { //提取
            if(empty($data['remark'])){
                $data['remark'] = '提现';
            }
            Store::where(['id'=>$data['sid']])->dec('bail',$data['amount'])->update();
        }else if ($data['type'] == 3) { //扣除
            if(empty($data['remark'])){
                //扣除原因不能为空
                return false;
            }
            Store::where(['id'=>$data['sid']])->dec('bail',$data['amount'])->update();
        }else{
            return false;
        }

        self::create($data);
        return true;
    }

    public static function pay_order($data){
        $bail =  StoreBail::find($data['id']);
        if(!empty($bail)){
            TechnicalBail::update(['id'=>$bail->id,'status'=>1]);
            Store::where(['id'=>$bail->sid])->dec('bail',$bail->amount)->update();
        }
    }
}
